package thread;

public class ThreadDemo9_5 {

	public static void main(String[] args) {
		Thread9 th1 = new Thread9(true);
		
		Thread t1 = new Thread(th1, "1");
		Thread t2 = new Thread(th1, "2");
		
		t1.start();
		t2.start();
	}
	
	
	

}
class Thread9 extends Thread {
	private int num = 0;
	private boolean flag;
	
	private static Object o = new Object();
	public Thread9(boolean flag){
		this.flag = flag;
	}
	@Override
	public void run() {
		while( true ){
			if( "1".equals( Thread.currentThread().getName() ) ){
				
				this.method1();
			} else {
				this.method2();
			}
		}
	}
	public  void method1(){
		synchronized( o ){
			num ++;
			System.out.println( "num++=" + num );
		}
	}
	public  void method2(){
		synchronized( o ){
			num--;
			System.out.println( "num--=" + num );
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	public int method3(){
		return num;
	}
}

